﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace ExcelOperateForNPOI
{
    public partial class Frm_SeleCol : Form
    {
        /// <summary>
        /// 选择的列表
        /// </summary>
        public Dictionary<string, string> MySeleCol = new Dictionary<string, string>();
        private Dictionary<string, string> aMyColName = new Dictionary<string, string>();

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="SeleColList"></param>
        public Frm_SeleCol(Dictionary<string, string> SeleColList)
        {
            InitializeComponent();

            ReversionHashTable(SeleColList);
            SeleListInit();
        }

        /// <summary>
        /// 初始化列表
        /// </summary>
        private void SeleListInit()
        {
            this.chListBox_Col.Items.Clear();

            foreach (KeyValuePair<string, string> iMyCol in this.aMyColName)
            {
                this.chListBox_Col.Items.Add(iMyCol.Key);
            }
        }

        private void btn_SeleAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.chListBox_Col.Items.Count; i++)
            {
                this.chListBox_Col.SetItemChecked(i,true);
            }
        }

        private void btn_NotSeleAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.chListBox_Col.Items.Count; i++)
            {
                this.chListBox_Col.SetItemChecked(i, false);
            }
        }

        private void btn_OrSele_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.chListBox_Col.Items.Count; i++)
            {
                this.chListBox_Col.SetItemChecked(i, (!(this.chListBox_Col.GetItemChecked(i))));
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.MySeleCol.Clear();
            string iMyItemText = "";

            for (int i = 0; i < this.chListBox_Col.CheckedItems.Count; i++)
            {
                iMyItemText = this.chListBox_Col.GetItemText(this.chListBox_Col.CheckedItems[i]);

                if (this.aMyColName.ContainsKey(iMyItemText))
                    this.MySeleCol.Add(this.aMyColName[iMyItemText],iMyItemText);
            }
            this.DialogResult = DialogResult.OK;
        }

        /// <summary>
        /// 反转哈希表
        /// </summary>
        /// <param name="RootHashTale">源表</param>
        private void ReversionHashTable(Dictionary<string, string> RootHashTale)
        {
            this.aMyColName.Clear();

            foreach (KeyValuePair<string, string> iMyCol in RootHashTale)
            {
                if ((!this.aMyColName.ContainsKey(iMyCol.Value)))
                    this.aMyColName.Add(iMyCol.Value, iMyCol.Key);
            }
        }
    }
}
